module NRSRSConfig
# using Tables
# 导出类型和主要函数
export nrSRSConfig

# 定义 SRS 配置结构体（可变以支持字段修改）
mutable struct nrSRSConfig
    # --- 公共可调字段 ---
    NumSRSPorts::Int
    SymbolStart::Int
    NumSRSSymbols::Int
    ResourceType::String
    SRSPeriod::Union{String,Vector{Int}}
    FrequencyStart::Int
    NRRC::Int
    CSRS::Int
    BSRS::Int
    BHop::Int
    Repetition::Int
    KTC::Int
    KBarTC::Int
    FrequencyScalingFactor::Int
    StartRBIndex::Int
    EnableStartRBHopping::Bool
    CyclicShift::Int
    GroupSeqHopping::String
    NSRSID::Int
    SRSPositioning::Bool
    EnableEightPortTDM::Bool
    CyclicShiftHopping::Bool
    CyclicShiftHoppingID::Int
    CyclicShiftHoppingSubset::Vector{Int}
    HoppingFinerGranularity::Bool
    CombOffsetHopping::Bool
    CombOffsetHoppingID::Int
    CombOffsetHoppingSubset::Vector{Int}
    HoppingWithRepetition::Bool

    # --- 只读字段（通过构造函数初始化）---
    NRB::Int
    NRBPerTransmission::Float64

    # --- 内部常量（不导出）---
    #BandwidthConfigurationTable SRS Bandwidth configuration table
    # Table containing the SRS configuration parameters m_SRS and N in
    # TS 38.211 Table 6.4.1.4.3-1.
    const BandwidthConfigurationTable::Dict{Any, Any}

    #SubcarrierOffsetTable Subcarrier offset for SRS user positioning
    # Table containing subcarrier offsets per OFDM symbol and
    # transmission comb number for SRS user positioning specified in TS
    # 38.211 Table 6.4.1.4.3-2.
    const SubcarrierOffsetTable::Dict{Any, Any}
    
    #StartRBHoppingTable Frequency hopping offset table
    # Table containing frequency hopping offset indices for partial
    # frequency sounding specified in TS 38.211 Table 6.4.1.4.3-3.
    const StartRBHoppingTable::Dict{Any, Any}
    

    # 构造函数
    function nrSRSConfig(;
        NumSRSPorts = 1,
        SymbolStart = 13,
        NumSRSSymbols = 1,
        ResourceType = "periodic",
        SRSPeriod = "on",
        FrequencyStart = 0,
        NRRC = 0,
        CSRS = 0,
        BSRS = 0,
        BHop = 0,
        Repetition = 1,
        KTC = 2,
        KBarTC = 0,
        FrequencyScalingFactor = 1,
        StartRBIndex = 0,
        EnableStartRBHopping = false,
        CyclicShift = 0,
        GroupSeqHopping = "neither",
        NSRSID = 0,
        SRSPositioning = false,
        EnableEightPortTDM = false,
        CyclicShiftHopping = false,
        CyclicShiftHoppingID = 0,
        CyclicShiftHoppingSubset = Int[],
        HoppingFinerGranularity = false,
        CombOffsetHopping = false,
        CombOffsetHoppingID = 0,
        CombOffsetHoppingSubset = Int[],
        HoppingWithRepetition = false
    )
        # 初始化只读字段
        NRB = calculate_NRB(CSRS, BHop, BSRS)
        NRBPerTransmission = calculate_NRB_per_transmission(CSRS, BSRS, FrequencyScalingFactor)
        BandwidthConfigurationTable = getBandwidthConfigTable()
        SubcarrierOffsetTable = getOffsetKTable()
        StartRBHoppingTable = getStartRBHoppingTable()
    
        # 创建实例并验证
        new(
            NumSRSPorts, SymbolStart, NumSRSSymbols, ResourceType, SRSPeriod,
            FrequencyStart, NRRC, CSRS, BSRS, BHop, Repetition, KTC, KBarTC,
            FrequencyScalingFactor, StartRBIndex, EnableStartRBHopping, CyclicShift,
            GroupSeqHopping, NSRSID, SRSPositioning, EnableEightPortTDM,
            CyclicShiftHopping, CyclicShiftHoppingID, CyclicShiftHoppingSubset,
            HoppingFinerGranularity, CombOffsetHopping, CombOffsetHoppingID,
            CombOffsetHoppingSubset, HoppingWithRepetition,
            NRB, NRBPerTransmission,  BandwidthConfigurationTable, 
            SubcarrierOffsetTable, StartRBHoppingTable
        )
    end
end

# --- 常量表生成函数（内部实现）---
function getBandwidthConfigTable(CSRS=nothing,BSRS=nothing)
    # 实现 TS 38.211 Table 6.4.1.4.3-1
    table64143_1 = Array{Int64}([
        0  4  1  4  1  4  1  4  1;
        1  8  1  4  2  4  1  4  1;
        2 12  1  4  3  4  1  4  1;
        3 16  1  4  4  4  1  4  1;
        4 16  1  8  2  4  2  4  1;
        5 20  1  4  5  4  1  4  1;
        6 24  1  4  6  4  1  4  1;
        7 24  1 12  2  4  3  4  1;
        8 28  1  4  7  4  1  4  1;
        9 32  1 16  2  8  2  4  2;
       10 36  1 12  3  4  3  4  1;
       11 40  1 20  2  4  5  4  1;
       12 48  1 16  3  8  2  4  2;
       13 48  1 24  2 12  2  4  3;
       14 52  1  4 13  4  1  4  1;
       15 56  1 28  2  4  7  4  1;
       16 60  1 20  3  4  5  4  1;
       17 64  1 32  2 16  2  4  4;
       18 72  1 24  3 12  2  4  3;
       19 72  1 36  2 12  3  4  3;
       20 76  1  4 19  4  1  4  1;
       21 80  1 40  2 20  2  4  5;
       22 88  1 44  2  4 11  4  1;
       23 96  1 32  3 16  2  4  4;
       24 96  1 48  2 24  2  4  6;
       25 104 1 52  2  4 13  4  1;
       26 112 1 56  2 28  2  4  7;
       27 120 1 60  2 20  3  4  5;
       28 120 1 40  3  8  5  4  2;
       29 120 1 24  5 12  2  4  3;
       30 128 1 64  2 32  2  4  8;
       31 128 1 64  2 16  4  4  4;
       32 128 1 16  8  8  2  4  2;
       33 132 1 44  3  4 11  4  1;
       34 136 1 68  2  4 17  4  1;
       35 144 1 72  2 36  2  4  9;
       36 144 1 48  3 24  2 12  2;
       37 144 1 48  3 16  3  4  4;
       38 144 1 16  9  8  2  4  2;
       39 152 1 76  2  4 19  4  1;
       40 160 1 80  2 40  2  4 10;
       41 160 1 80  2 20  4  4  5;
       42 160 1 32  5 16  2  4  4;
       43 168 1 84  2 28  3  4  7;
       44 176 1 88  2 44  2  4 11;
       45 184 1 92  2  4 23  4  1;
       46 192 1 96  2 48  2  4 12;
       47 192 1 96  2 24  4  4  6;
       48 192 1 64  3 16  4  4  4;
       49 192 1 24  8  8  3  4  2;
       50 208 1 104 2 52  2  4 13;
       51 216 1 108 2 36  3  4  9;
       52 224 1 112 2 56  2  4 14;
       53 240 1 120 2 60  2  4 15;
       54 240 1 80  3 20  4  4  5;
       55 240 1 48  5 16  3  8  2;
       56 240 1 24 10 12  2  4  3;
       57 256 1 128 2 64  2  4 16;
       58 256 1 128 2 32  4  4  8;
       59 256 1 16 16  8  2  4  2;
       60 264 1 132 2 44  3  4 11;
       61 272 1 136 2 68  2  4 17;
       62 272 1 68  4  4 17  4  1;
       63 272 1 16 17  8  2  4  2;
    ])

    if isnothing(CSRS) && isnothing(BSRS)
        ConfTable=Dict{Any, Any}(
            "C_SRS" => table64143_1[:,1],
            "m_SRS_0" => table64143_1[:,2],
            "N_0" => table64143_1[:,3],
            "m_SRS_1" => table64143_1[:,4],
            "N_1" => table64143_1[:,5],
            "m_SRS_2" => table64143_1[:,6],
            "N_2" => table64143_1[:,7],
            "m_SRS_3" => table64143_1[:,8],
            "N_3" => table64143_1[:,9]
        )
        return  ConfTable
    else
        return table64143_1[CSRS + 1, 2*(BSRS+1)]'
    end
end

function getOffsetKTable()
    # 实现 TS 38.211 Table 6.4.1.4.3-2
    return Dict{Any,Any}(
      "K_TC" => [2;4;8],
      "NSRS_symb = 1" => [0;NaN;NaN],
      "NSRS_symb = 2" => [0 1;0 2;NaN NaN],
      "NSRS_symb = 4" => [0 1 0 1;0 2 1 3;0 4 2 6],
      "NSRS_symb = 8" => [NaN NaN NaN NaN NaN NaN NaN NaN; 
                          0   2   1   3   0   2   1   3;
                          0   4   2   6   1   5   3   7;],
      "NSRS_symb = 12" =>[NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN;
                          0   2   1   3   0   2   1   3    0   2   1   3;
                          0   4   2   6   1   5   3   7    0   4   2   6],
    )
end

function getStartRBHoppingTable()
    confTable = [
        0 0 0 0;
        1 NaN 1 2;
        2 NaN NaN 1;
        3 NaN NaN 3
    ]
    kHop = Dict{Any,Any}(
        "kBarHop" => confTable[:, 1],
        "PF = 1" => confTable[:, 2],
        "PF = 2" => confTable[:, 3],
        "PF = 4" => confTable[:, 4]
    )
    return kHop

end

# Read-only properties getters
function calculate_NRB(CSRS, BHop, BSRS)
 
    if BHop < BSRS # Frequency hopping cases
        val = getBandwidthConfigTable(CSRS,BHop);
    else
        val = getBandwidthConfigTable(CSRS,BSRS);
    end
    return val
end

function calculate_NRB_per_transmission(CSRS, BSRS, FrequencyScalingFactor)
    val =  getBandwidthConfigTable(CSRS,BSRS)/Float64(FrequencyScalingFactor);
    return val
end

function Base.setproperty!(obj::nrSRSConfig, name::Symbol, value)
    if name == :CSRS
       obj.NRB=calculate_NRB(value, obj.BHop, obj.BSRS)
       obj.NRBPerTransmission=calculate_NRB_per_transmission(value, obj.BSRS, obj.FrequencyScalingFactor)
       setfield!(obj, name, value)
    elseif name == :BSRS
        obj.NRB=calculate_NRB(obj.CSRS, obj.BHop, value)
        obj.NRBPerTransmission=calculate_NRB_per_transmission(obj.CSRS, value, obj.FrequencyScalingFactor)
        setfield!(obj, name, value)
    elseif name == :BHop
        obj.NRB=calculate_NRB(obj.CSRS, value, obj.BSRS)
        setfield!(obj, name, value)
    elseif name == :FrequencyScalingFactor
        obj.NRBPerTransmission=calculate_NRB_per_transmission(obj.CSRS, obj.BSRS, value)
        setfield!(obj, name, value)
    else
        setfield!(obj, name, value)
    end
end

# # --- 验证方法 ---
# function validate_config(srs::nrSRSConfig)
#     # 实现 MATLAB 中的 validateConfig 逻辑
#     return (
#         NumSRSPorts = srs.NumSRSPorts,
#         SymbolStart = srs.SymbolStart,
#         # ... 其他字段
#     )
# end

# # --- 属性验证（通过构造函数或 setter）---
# function Base.setproperty!(srs::nrSRSConfig, name::Symbol, value)
#     if name === :SRSPeriod
#         validate_SRSPeriod(value) || error("Invalid SRSPeriod")
#     elseif name === :ResourceType
#         value in ["periodic", "semi-persistent", "aperiodic"] || error("Invalid ResourceType")
#     end
#     # ... 其他属性验证
#     setfield!(srs, name, value)
# end

# function validate_SRSPeriod(val)
#     if val isa String
#         return val in ["on", "off"]
#     elseif val isa Vector{Int}
#         return length(val) == 2 && val[1] in SRSPERIOD_OPTIONS && val[2] < val[1]
#     end
#     return false
# end


end # module SRSConfig